/*
 * author      : 吴中勤 
 * date        : 2018/8/18
 * description : TODO
 */

package com.deepexi.adapter.common;

import org.apache.commons.io.IOUtils;

import java.io.IOException;

public class ResourceUtils {

    /** 读取资源文件 -> string */
    public static String loadRes2String(String fileName) {
        String result = null;

        ClassLoader classLoader = ResourceUtils.class.getClassLoader();
        try {
            result = IOUtils.toString(classLoader.getResourceAsStream(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

//    public static String loadRes2String(String path) {
//        try {
//            File file = ResourceUtils.getFile("classpath:" + path);
//
//            //File found
//            //System.out.println("File Found : " + file.exists());
//
//            //Read File Content
//            String content = new String(Files.readAllBytes(file.toPath()));
//            // System.out.println(content);
//            return content;
//        } catch (IOException ex) {
//            throw new RuntimeException("文件未找到:" + path);
//        }
//    }

    /** 将资源文件转换为对象, 便于测试 */
    public static <T> T loadRes2Object(String path, Class<T> clazz) {
        String str = loadRes2String(path);
        return CommonUtils.jsonString2Obj(str, clazz);
    }
}
